(function () {
  Array.prototype.removeRepeatBuildingAndFLoor = function () {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].buildingName == res[j].buildingName && this[i].floorNum == res[j].floorNum) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };
  new Vue({
    el: '#turnoverDetail',
    data() {
      return {
        tabList: [{
          code: '',
          name: '营业额详情'
        }],
        currentTab: '营业额详情', // 当前选中tab
        data: '',
        isUpdate: false,
        updateData: '',//修改的数据
        customerDetail: '',//客户详情
        projectName: '',//项目名称
        totalArea: '',//面积
        building: [],//项目楼栋楼层单元
        turnoverId: hlUrlUtil.GetQueryString('id'),
        unitId: null, // 单元id
        unitDetailsFlag: false,
        permissions: JSON.parse(localStorage.getItem('permissions')),
        HlValidator: '',
      }
    },
    mounted() {
      this.getDetailData()
      this.HlValidator = new HlValidator()
    },
    filters: {
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatDatehms: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
      },
      formatUndefined: function (item) {
        if (!item) return '--'
        return item
      },
      formatNum: function (item) {
        if (item === undefined) return '--'
        return hlNumberUtil.formatNum(item)
      }
    },
    methods: {
      //****数据区****/
      //获取详情数据
      getDetailData() {
        hlhttp.get({
          url: 'turnover_info',
          data: { id: this.turnoverId },
          success: (res) => {
            this.data = res
            res.memo = res.memo ? res.memo : ''
            this.getcustomerDetail(res.customerId)
            this.getContractDetail(res.contractId)
          }
        })
      },
      // 获取客户详情
      getcustomerDetail(id) {
        hlhttp.get({
          url: 'customer,cincb_info',
          data: { customer_id: id },
          success: (res) => {
            this.customerDetail = res
          }
        })
      },
      //获取合同详情
      getContractDetail(id) {
        hlhttp.get({
          url: 'contract',
          data: { id: id },
          success: (res) => {
            let buildingData = this.unitData(res.units)
            this.totalArea = res.rentAcreage
            this.building = buildingData.building
            this.projectName = res.projectName
          }
        })
      },
      //****操作区****/
      // 切换导航
      switchTab(val) {
        this.currentTab = val
      },
      //修改按钮
      updateBut() {
        this.isUpdate = true
        this.updateData = JSON.parse(JSON.stringify(this.data))
      },
      // 确定修改营业额
      updateTurnoverInfo() {
        this.HlValidator.add(this.updateData.turnoverAmount, [
          {
            rule: 'isEmpty',
            msg: '请输入金额'
          },
          {
            rule: 'isAmount',
            msg: '请输入正确金额'
          }])
        var errorMsg = this.HlValidator.start()
        if (errorMsg != true) {
          hl.toast(errorMsg)
          return false
        }
        hlhttp.put({
          url: 'turnover_info',
          data: {
            'id': this.turnoverId,
            "turnover_amount": this.updateData.turnoverAmount,
            "memo": this.updateData.memo
          },
          success: (res) => {
            hl.toast('修改成功')
            this.isUpdate = false
            this.getDetailData();
          }
        })
      },
      // 获取单元详情
      getUnitDetails: function (unitId) {
        var self = this;
        self.unitId = unitId;
        self.unitDetailsFlag = true;
      },
      closeUnitPop: function (state) {
        this.unitId = null;
        this.unitDetailsFlag = false;
      },
      unitData: function (unitsData) {
        // 去除含有工位车位的单元
        var units = [];
        unitsData.map(function (_item) {
          if (_item.positions.length === 0) {
            units.push(_item)
          }
        })
        if (units.length === 0) {
          return {
            building: [],
            totalArea: 0
          }
        }
        // 合并单元项
        var unit = [];
        var totalArea = 0;
        for (var i = 0; i < units.length; i++) {
          unit.push({
            buildingName: units[i].buildingName,
            floorNum: units[i].floorNum,
            floorName: units[i].floorName,
            unit: [],
            unitId: []
          })
        }
        var unitArr = unit.removeRepeatBuildingAndFLoor()
        for (var z = 0; z < unitArr.length; z++) {
          for (var m = 0; m < units.length; m++) {
            if (units[m].buildingName == unitArr[z].buildingName && units[m].floorNum == unitArr[z].floorNum) {
              unitArr[z].unit.push(units[m].unitName + '(' + hlNumberUtil.formatNum(units[m].unitAcreage) + 'm²)')
              unitArr[z].unitId.push(units[m].unitId)
              totalArea = totalArea + Number(units[m].unitAcreage)
            }
          }
        }
        // 合并楼层项
        var building = [];
        for (var j = 0; j < unitArr.length; j++) {
          building.push({
            buildingName: unitArr[j].buildingName,
            floor: []
          })
        }
        var buildingArr = hlArrayUtil.removeRepeat(building, 'buildingName')
        for (var k = 0; k < buildingArr.length; k++) {
          for (var n = 0; n < unitArr.length; n++) {
            if (unitArr[n].buildingName == buildingArr[k].buildingName) {
              buildingArr[k].floor.push({
                'floorNum': unitArr[n].floorNum,
                'floorName': unitArr[n].floorName,
                'unit': unitArr[n].unit,
                'unitId': unitArr[n].unitId
              })
            }
          }
        }
        return {
          building: buildingArr,
          totalArea: totalArea
        }
      },
    },
  })
}())
